package com.huiquan.analysis.dao;

import com.huiquan.analysis.constants.AnalysisSentenceConstant;
import com.huiquan.analysis.domain.AnalysisVocabulary;
import com.huiquan.analysis.domain.AnalysisVocabularySourceDto;
import com.huiquan.framework.base.EnhancedBaseDao;
import com.ibatis.sqlmap.client.SqlMapClient;

import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AnalysisVocabularyDao extends EnhancedBaseDao<AnalysisVocabulary> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "ANALYSIS_VOCABULARY.";

	public AnalysisVocabularyDao() {
		super(space);
	}

	public List<AnalysisVocabulary> retriveListBySid(Long id) {
		Map<String, Object> param = new HashMap<>();
		param.put("sid", id);

		return this.retrieveList(param);
	}

	public void deleteVocabulaysBySid(Long sid) {
		this.delete(space + "DELETE_VOCABULARY_BY_SID", sid, sqlMapClient);
	}

	public void batchInsert(List<AnalysisVocabulary> vocabularys) {
		this.insert(space + "BATCH_INSERT", vocabularys, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<AnalysisVocabulary> retrieveUnlabeledCtVocabularys() {
		Map<String, Object> param = new HashMap<>();
		param.put("type", AnalysisSentenceConstant.TYPE_CT);

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_UNLABELED_VOCABULARYS", param, sqlMapClient);
	}
	
	/**
	 * 
	 * @param type
	 *            1:主诉2:现病史3:医学影像
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AnalysisVocabulary> retrieveUnlabeledVocabularys(int type) {
		Map<String, Object> param = new HashMap<>();
		param.put("type", type);

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_UNLABELED_VOCABULARYS", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<AnalysisVocabulary> retrieveLabeledCtList() {
		Map<String, Object> param = new HashMap<>();
		param.put("type", AnalysisSentenceConstant.TYPE_CT);

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_LABELED_VOCABULARYS", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<AnalysisVocabulary> retrieveLabeledZsList() {
		Map<String, Object> param = new HashMap<>();

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_LABELED_ZS_VOCABULARYS", param, sqlMapClient);
	}

	public List<AnalysisVocabulary> retrieveCtList() {
		Map<String, Object> param = new HashMap<>();
		param.put("type", AnalysisSentenceConstant.TYPE_CT);

		return this.retrieveList(param);
	}

	/**
	 * 得到最近一段时间内的未标注的词语
	 * 
	 * @param min
	 *            分钟数
	 * @param type
	 *            类型：1、主诉，2、现病史，3、医学影像
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AnalysisVocabulary> retrieveUnlabeledVocabularysByLastMin(int min, int type) {
		Map<String, Object> param = new HashMap<>();
		param.put("type", type);
		param.put("min", min);

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_UNLABELED_VOCABULARYS", param, sqlMapClient);
	}

	public void initCtBme() {
		this.update(space + "INIT_CT_BME", sqlMapClient);
	}

	public void initZsBme() {
		this.update(space + "INIT_ZS_BME", sqlMapClient);
	}

	public void initXbsBme() {
		this.update(space + "INIT_XBS_BME", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<AnalysisVocabularySourceDto> retrieveSourceDtoByVids(Long sid) {
		return (List<AnalysisVocabularySourceDto>) this.list(space + "RETRIEVE_SOURCE_DTO_BY_VIDS", sid, sqlMapClient);
	}

	public void batchInsertSource(List<AnalysisVocabularySourceDto> sourceList) {
		this.insert(space + "BATCH_INSERT_SOURCE", sourceList, sqlMapClient);
	}

	public void deleteSourceBySid(Long sid) {
		this.delete(space + "DELETE_SOURCE_BY_SID", sid, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrieveSourceErrMsg() {
		return (List<Map<String, String>>) this.list(space + "RETRIEVE_SOURCE_ERR_MSG", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<AnalysisVocabulary> retrieveUnlabeledXbsVocabularysByLastMin(int min, String sencondType) {
		Map<String, Object> param = new HashMap<>();
		param.put("sencondType", sencondType);
		param.put("min", min);

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_UNLABELED_XBS_VOCABULARYS", param, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<AnalysisVocabulary> retrieveDelList(Map<String, Object> param) {
		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_UNLABELED_VOCABULARYS", param, sqlMapClient);
	}


	public List<AnalysisVocabulary> retriveSubListBySid(Long id) {
		Map<String, Object> param = new HashMap<>();
		param.put("sid", id);

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_SUBDIVISION_VOCABULARYS", param, sqlMapClient);
	}


	public void batchInsertSubdivisionVocab(List<AnalysisVocabulary> vocabularys,Long newId,Integer type) {
		for(AnalysisVocabulary vocabulary:vocabularys){
			vocabulary.setSid(newId);
			vocabulary.setType(type);
		}
		this.insert(space + "BATCH_INSERT_SUBDIVISION_VOCAB", vocabularys, sqlMapClient);
	}

	public void deleteSubdivisionVocab() {
		this.delete(space + "DELETE_VOCABULARY_BY_SID", sqlMapClient);
	}

	public void deleteSubVocabulaysBySid(Long sid) {
		this.delete(space + "DELETE_SUBDIVISION_BY_SID", sid , sqlMapClient);
	}


	public void batchInsertEnlarge(List<AnalysisVocabulary> vocabularys) {
		this.insert(space + "BATCH_INSERT_ENLARGE", vocabularys, sqlMapClient);
	}
	public void deleteEnlarge() {
		this.update(space + "DELETE_ENLARGE", sqlMapClient);
	}
	public int updateSubVocab(AnalysisVocabulary entity) {
		return update(space + "UPDATE_SUB_VOCAB", entity, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<AnalysisVocabulary> retrieveUnlabeledXbsVocabularys(String sencondType) {
		Map<String, Object> param = new HashMap<>();
		param.put("secondType", sencondType);

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_UNLABELED_XBS_VOCABULARYS", param, sqlMapClient);
	}

	public List<AnalysisVocabulary> retrieveLabeledXbsVocabularys(String sencondType) {
		Map<String, Object> param = new HashMap<>();
		param.put("secondType", sencondType);

		return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_LABELED_XBS_VOCABULARYS", param, sqlMapClient);
	}
	 
    /**
     * 根据id从未删除表获取vocab对象
     *
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<AnalysisVocabulary> retrieveVocabById(Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_VOCAB_BY_SID", param, sqlMapClient);
    }
    
    /**
     * 根据id从已删除表获取vocab对象
     *
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<AnalysisVocabulary> retrieveDelVocabById(Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_DEL_VOCAB_BY_SID", param, sqlMapClient);
    }
}
